package com.lyr.controller;

import com.lyr.pojo.Obj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author liyiruo
 * @Description
 * @Date 2023/10/23 下午2:36
 */
@Controller
public class MyController {
    private static Logger logger = LoggerFactory.getLogger(MyController.class);

    /**
     * 返回的页面都在templates下
     * <p>
     * 方法返回值 String -》页面
     * 参数 map里可以传 要返回到页面的值
     * ModelAndView里可以传多个参数和一个页面
     *
     * @return
     */
    @RequestMapping("/hello")
    public ModelAndView getToHello(Map map) {
        logger.info("====>{}", "hello");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("name", "张三");

        map.put("age", "20");
        return modelAndView;
    }


    @RequestMapping("/word")
    @ResponseBody
    public Map getToWorld(@RequestBody Obj obj) {
        logger.info("====>{}", obj.getS());
        Map map = new HashMap();
        map.put("111", "ggg");
        map.put("2222", "hhh");
        map.put("333", obj);

        return map;
    }


    @RequestMapping("/test")
    @ResponseBody
    public Map<String, Object> getTest(HttpServletRequest req, HttpServletResponse res) {
        HttpSession session = req.getSession();
        boolean aNew = session.isNew();
        long creationTime = session.getCreationTime();
        Date date = new Date(creationTime);
        String id = session.getId();

        Map map = new HashMap();
        map.put("isnew", aNew);
        map.put("date", date);
        map.put("id", id);
        logger.debug("{}", map);
        logger.debug("session=>{}", session);
        Collection<String> names = res.getHeaderNames();

        for (String s : names) {
            logger.info("====>{}", s);
            System.out.println("====>{}" + s);
            String header = res.getHeader(s);
            System.out.println(header);
        }
        ServletContext servletContext = req.getServletContext();
        String path = servletContext.getContextPath();
        Set<String> paths = servletContext.getResourcePaths("/");

        for (String s :
                paths) {
            System.out.println("s===>"+s);
        }
        System.out.println("-----path-------");
        System.out.println(path);
        return map;
    }


}
